package com.jianbing.shasha.controller;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.jianbing.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.jianbing.common.annotation.Log;
import com.jianbing.common.core.controller.BaseController;
import com.jianbing.common.core.domain.AjaxResult;
import com.jianbing.common.enums.BusinessType;
import com.jianbing.shasha.domain.ShashaUserRecord;
import com.jianbing.shasha.service.IShashaUserRecordService;
import com.jianbing.common.utils.poi.ExcelUtil;
import com.jianbing.common.core.page.TableDataInfo;

/**
 * 用户学习测试记录Controller
 * 
 * @author ruoyi
 * @date 2024-06-09
 */
@RestController
@RequestMapping("/shasha/record")
public class ShashaUserRecordController extends BaseController
{
    @Autowired
    private IShashaUserRecordService shashaUserRecordService;

    /**
     * 查询用户学习测试记录列表
     */
    //  @PreAuthorize("@ss.hasPermi('shasha:record:list')")
    @GetMapping("/list")
    public TableDataInfo list(ShashaUserRecord shashaUserRecord)
    {
        startPage();
        List<ShashaUserRecord> list = shashaUserRecordService.selectShashaUserRecordList(shashaUserRecord);
        return getDataTable(list);
    }

    /**
     * 导出用户学习测试记录列表
     */
    //   @PreAuthorize("@ss.hasPermi('shasha:record:export')")
    @Log(title = "用户学习测试记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ShashaUserRecord shashaUserRecord)
    {
        List<ShashaUserRecord> list = shashaUserRecordService.selectShashaUserRecordList(shashaUserRecord);
        ExcelUtil<ShashaUserRecord> util = new ExcelUtil<ShashaUserRecord>(ShashaUserRecord.class);
        util.exportExcel(response, list, "用户学习测试记录数据");
    }

    /**
     * 获取用户学习测试记录详细信息
     */
    //  @PreAuthorize("@ss.hasPermi('shasha:record:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(shashaUserRecordService.selectShashaUserRecordById(id));
    }

    /**
     * 新增用户学习测试记录
     */
    //   @PreAuthorize("@ss.hasPermi('shasha:record:add')")
    @Log(title = "用户学习测试记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ShashaUserRecord shashaUserRecord)
    {
        Long userId = SecurityUtils.getUserId();
        shashaUserRecord.setUserId(userId);
        shashaUserRecord.setInsertTime(new Date());
        return toAjax(shashaUserRecordService.insertShashaUserRecord(shashaUserRecord));
    }

    /**
     * 修改用户学习测试记录
     */
    //  @PreAuthorize("@ss.hasPermi('shasha:record:edit')")
    @Log(title = "用户学习测试记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ShashaUserRecord shashaUserRecord)
    {
        return toAjax(shashaUserRecordService.updateShashaUserRecord(shashaUserRecord));
    }

    /**
     * 删除用户学习测试记录
     */
    //  @PreAuthorize("@ss.hasPermi('shasha:record:remove')")
    @Log(title = "用户学习测试记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(shashaUserRecordService.deleteShashaUserRecordByIds(ids));
    }

    //  @PreAuthorize("@ss.hasPermi('shasha:record:addConsolidate')")
    @Log(title = "用户巩固记录添加", businessType = BusinessType.INSERT)
    @PostMapping("/consolidate")
    public AjaxResult addConsolidate(@RequestBody ShashaUserRecord shashaUserRecord)
    {
        Long userId = SecurityUtils.getUserId();
        shashaUserRecord.setUserId(userId);
        shashaUserRecord.setInsertTime(new Date());
        shashaUserRecord.setTestType("consolidate");
        return toAjax(shashaUserRecordService.insertShashaUserRecord(shashaUserRecord));
    }
}
